package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;

import com.manger.model.Project;

import com.manger.model.ZX;
import com.manger.utils.CopyFiles;
import com.manger.utils.LoanFileEnum;
import com.manger.utils.SystemConstant;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ZXCaseFilesFormat
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/9/17 13:11
 * @Version 1.0
 */
public class ZXCaseFilesFormat implements CaseFilesFormat{

    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        String excelFilePath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        File excelFile = new File(excelFilePath);
        System.out.println(excelFile.getCanonicalPath());
        if(!excelFile.exists()){
            System.out.println("没有找到委托信息！");
            return;
        }
        Map<String , List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(excelFile, ZX.class).get();
        if(map == null || map.isEmpty()){
            return;
        }
        List<ZX> zxList = (List<ZX>)  map.get(map.keySet().iterator().next());
        if(zxList == null || zxList.isEmpty()){
            System.out.println("没有解析到委托信息！");
            return;
        }
        Map<String,ZX> resultMap = new HashMap<>();
        for(ZX zx : zxList){
            resultMap.put(zx.getIdCard(),zx);
        }
        String mPath = extractDir + "/" + SystemConstant.CASE_FILE_DIR_NAME ;
        File file = new File(mPath);
        File[] files = file.listFiles();
        for(File f : files){
            File[] childFiles = f.listFiles();
            for(File childFile : childFiles){
                String fileName = childFile.getName();
                ZX zx = null;
                for(String key: resultMap.keySet()){
                    if(fileName.contains(key)){
                      zx = resultMap.get(key);
                      break;
                    }
                }
                if(zx == null){
                    continue;
                }
                String resourcePath = extractDir + "/" + project.getDebtName() + "/" + zx.getCustomName() + "-" + zx.getIdCard();
                File resourceFile = new File(resourcePath);
                if(!resourceFile.exists()){
                    continue;
                }
                String targetPath = targetDir+ "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME + "/" + zx.getCustomName() + "-" + zx.getIdCard();
                File targetFile = new File(targetPath);
                if(!targetFile.exists()){
                    targetFile.mkdirs();
                }
                CopyFiles.copyDirToDir(resourcePath, targetPath + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
                CopyFiles.copyFileToDir(childFile.getCanonicalPath(),targetPath + "/" + f.getName() );
                CopyFiles.copyBaseFiles(targetPath, project);
                System.out.println("整理案件：【" + zx.getCustomName() + "-" + zx.getIdCard() + "】" );
            }
        }
    }

    public static void main(String[] args) throws Exception{
        ZXCaseFilesFormat zxCaseFilesFormat = new ZXCaseFilesFormat();
        Project project = new Project();
        project.setDebtName("中信");
        String extractDir = "D:\\BaiduNetdiskDownload\\中信\\中信\\中信";
        String targetDir = "C:\\Users\\liuhj\\Desktop\\caseTest";
        zxCaseFilesFormat.format(extractDir,targetDir,project);
    }
}
